package state.component;

import iplayparticle.view.iplayparticleResources;

import java.util.Vector;

import particle.core.tools.PMath;

import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.GroupForm;
import state.Sprite;
import state.bean.GameRect;
import state.font.ImageFont;
import state.util.Hashtable;

public class MultiTab extends Component {
  public static final String OPEN_TAB_EVENT = "openTab";

  private Vector titles = new Vector(); // String
  private Vector tabs = new Vector(); // GameForm

  private int headerHeight = 20;
  private int currentSelectedTab = 0;
  private boolean selectedTabRepeat = false;

  private GameImage tabImage = null;
  private GameImage tabFocusImage = null;
  private Hashtable parametersStore = null;

  private GroupForm groupForm = null;

  public MultiTab(GroupForm groupForm1, int width, int height) {
    super(groupForm1.getManager(), 0, 0, width, height);
    this.groupForm = groupForm1;
    tabImage = new GameImage(iplayparticleResources.image_tab, null, null);
    tabFocusImage = new GameImage(iplayparticleResources.image_tab_focus, null, null);
    setFocusable(true);
    setFocused(true);
  }

  public void setTabImage(GameImage tabImage1) {
    this.tabImage = tabImage1;
  }

  public void setTabFocusImage(GameImage tabFocusImage1) {
    this.tabFocusImage = tabFocusImage1;
  }

  public int addTab(String title, GameForm tab) {
    return addTab(title, tab, null);
  }

  public int addTab(String title, GameForm tab, Hashtable parameters) {
    parametersStore = parameters;
    
    if (title == null) {
      System.out.println("MultiTab : addTab : title must be not null");
      title = "";
    }
    
    if (tab == null) {
      System.out.println("MultiTab : addTab : tab must be not null");
      return 0;
    }
    
    titles.addElement(title);
    tabs.addElement(tab);
    tab.setParentGroup(groupForm);
    tab.init(parameters);
    tab.isRunning = true;
    if (GameGlobal.systemCanvas.isSetTouchPadForAllForm()) {
      tab.getManager().enableTouchPad();
    }
    return tabs.size() - 1;
  }

  public void setTab(int index, GameForm gameForm, Hashtable parameters) {
    tabs.setElementAt(gameForm, index);
    gameForm.setParentGroup(groupForm);
    gameForm.init(parameters);
    gameForm.isRunning = true;
  }

  public void setTitle(int index, String title) {
    titles.setElementAt(title, index);
  }

  public void removeTab(int index) {
    if (index > tabs.size() - 1) {
      return;
    }
    titles.removeElementAt(index);
    tabs.removeElementAt(index);

    if (currentSelectedTab > tabs.size() - 1) {
      currentSelectedTab = tabs.size() - 1;
    }
  }

  public int getHeaderHeight() {
    return headerHeight;
  }

  public void setHeaderHeight(int headerHeight1) {
    this.headerHeight = headerHeight1;
  }

  public int getCurrentSelectedTab() {
    return currentSelectedTab;
  }

  public void setCurrentSelectedTab(int currentSelectedTab1) {
    this.currentSelectedTab = currentSelectedTab1;
    checkToInitCurrentTab();
  }

  public boolean isSelectedTabRepeat() {
    return selectedTabRepeat;
  }

  public void setSelectedTabRepeat(boolean isSelectedTabRepeat) {
    this.selectedTabRepeat = isSelectedTabRepeat;
  }

  public GameForm getCurrentTab() {
    return (GameForm) tabs.elementAt(currentSelectedTab);
  }

  public void nextTab() {
    if (selectedTabRepeat) {
      currentSelectedTab = (currentSelectedTab + 1) % tabs.size();
    } else {
      currentSelectedTab = PMath.min(++currentSelectedTab, (tabs.size() - 1));
    }
    checkToInitCurrentTab();
    callBackEvent(tabs.elementAt(currentSelectedTab), OPEN_TAB_EVENT);
  }

  private void checkToInitCurrentTab() {
    GameForm currentTab = getCurrentTab();
    if (!currentTab.isRunning) {
      currentTab.init(parametersStore);
      currentTab.isRunning = true;
    }
  }

  public void previousTab() {
    if (selectedTabRepeat) {
      if (--currentSelectedTab < 0) {
        currentSelectedTab = (tabs.size() - 1);
      }
    } else {
      if (--currentSelectedTab < 0) {
        currentSelectedTab = 0;
      }
    }
    checkToInitCurrentTab();
    callBackEvent(tabs.elementAt(currentSelectedTab), OPEN_TAB_EVENT);
  }

  public void selectTab(int index) {
    if (index < tabs.size()) {
      currentSelectedTab = index;
      checkToInitCurrentTab();
      callBackEvent(tabs.elementAt(currentSelectedTab), OPEN_TAB_EVENT);
    }
  }

  private void callBackEvent(Object tab, String action) {
    EventListener listener = (EventListener) tab;
    Event event1 = new Event(this, action);
    listener.onActionPerform(event1);
  }

  public void draw(Graphics g) {
    // Draw background
    if (_fillBackGround) {
      g.setColor(_backgroundColor);
      g.fillRect(_realX, _realY + headerHeight, _width, _height - headerHeight);
    }

    // Draw current selected tab content
    g.setClip(_realX, _realY + headerHeight, _width, _height - headerHeight);
    GameForm form = (GameForm) tabs.elementAt(currentSelectedTab);
    form.updateFullScreen();
    g.setClip(0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);

    
    // Draw tab header
    int tabWidth = _width / titles.size();
    if (tabWidth > tabImage.getWidth()) {
      tabWidth = tabImage.getWidth();
    }
    tabWidth = tabWidth + 2;
    g.setClip(_realX, 0, _width, headerHeight);
    for (int i = 0; i < titles.size(); i++) {
      String title = (String) titles.elementAt(i);
      ImageFont font = getFont();
      int titleWidth = font.stringWidth(title);
      if (i == currentSelectedTab) {
        g.drawImage(tabFocusImage, _realX + i * tabWidth, _realY, GameConstants.TOP_LEFT_ANCHOR);
        if (titleWidth > tabFocusImage.getWidth()) {
          font.drawString2(g, title, _focusFgColor, _realX + i * tabWidth + 5, _realY + 2, GameConstants.TOP_LEFT_ANCHOR);
        } else {
          font.drawString2(g, title, _focusFgColor, _realX + i * tabWidth + tabWidth / 2, _realY + 2, GameConstants.TOP_HCENTER_ANCHOR);
        }
      } else {
        g.drawImage(tabImage, _realX + i * tabWidth, _realY, GameConstants.TOP_LEFT_ANCHOR);
        if (titleWidth > tabFocusImage.getWidth()) {
          font.drawString2(g, title, _forceGroundColor, _realX + i * tabWidth + 5, _realY + 2, GameConstants.TOP_LEFT_ANCHOR);
        } else {
          font.drawString2(g, title, _forceGroundColor, _realX + i * tabWidth + tabWidth / 2, _realY + 2, GameConstants.TOP_HCENTER_ANCHOR);
        }
      }
    }
    g.setClip(0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
    
    // Draw border
    if (_hasBorder) {
      g.setColor(borderColor);
      g.drawRect(_realX, _realY + headerHeight, _width - 1, _height - headerHeight);
    }
  }

  public void keyPressed(Sprite source, int keyCode) {
    if (_focusable && _focused) {
      if (tabs.size() > 0) {
        ((GameForm) tabs.elementAt(currentSelectedTab)).doKeyPressed(keyCode);
      }
    }
  }

  public void keyReleased(Sprite source, int keyCode) {
    if (_focusable && _focused) {
      if (tabs.size() > 0) {
        ((GameForm) tabs.elementAt(currentSelectedTab)).doKeyReleased(keyCode);
      }
    }
  }

  public void keyRepeated(Sprite source, int keyCode) {
    if (_focusable && _focused) {
      if (tabs.size() > 0) {
        ((GameForm) tabs.elementAt(currentSelectedTab)).doKeyRepeated(keyCode);
      }
    }
  }

  public void pointerPressed(Sprite source, int x1, int y1) {
    if (tabs.size() > 0) {
      ((GameForm) tabs.elementAt(currentSelectedTab)).doPointerPressed(x1, y1);
    }
  }

  public void pointerReleased(Sprite source, int x1, int y1) {
    if (tabs.size() > 0) {
      ((GameForm) tabs.elementAt(currentSelectedTab)).doPointerReleased(x1, y1);
    }

    if (titles == null) {
      return;
    }

    // Check if user click to tab
    int tabWidth = _width / titles.size();
    if (tabWidth > tabImage.getWidth()) {
      tabWidth = tabImage.getWidth();
    }
    for (int i = 0; i < titles.size(); i++) {
      GameRect rect = new GameRect(_realX + i * tabWidth, _realY, tabImage.getWidth(), tabImage.getHeight());
      if (rect.contains(x1, y1)) {
        selectTab(i);
        return;
      }
    }
  }

  public Sprite detroy() {
    super.detroy();
    tabImage = null;
    tabFocusImage = null;
    titles = null;
    tabs = null;
    return this;
  }
}
